package com.yanfan.oauth.controller;

import cn.hutool.core.collection.CollUtil;
import com.yanfan.common.annotation.Anonymous;
import com.yanfan.common.core.domain.AjaxResult;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Map;

@Slf4j
@Anonymous
@RestController
@RequestMapping("/portal")
public class PortalController {


    @SneakyThrows
    @RequestMapping("baidu")
    public AjaxResult baidu(HttpServletRequest request) {
        Map<String, String[]> params = request.getParameterMap();
        log.info("【小度自定义技能】测试 params: {}", params);
        if (!CollUtil.isEmpty(params)) {
            params.keySet().forEach(key -> {
                log.info("【小度自定义技能】 " + key + ": " + Arrays.toString(params.get(key)));
            });
        } else {
            log.info("【小度自定义技能】 params为空");
        }

        BufferedReader streamReader = new BufferedReader(new InputStreamReader(request.getInputStream(), StandardCharsets.UTF_8));
        StringBuilder responseStrBuilder = new StringBuilder();
        String inputStr;
        while ((inputStr = streamReader.readLine()) != null)
            responseStrBuilder.append(inputStr);

        log.info("【小度自定义技能】 responseStrBuilder: {}", responseStrBuilder);
        return AjaxResult.success();
    }

}
